package com.sdry.web.controller.llm;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.sdry.model.llm.ImgResult;

/** 
* 图片上传
* @author lian 
* @version 1.0
* @time 2018年11月8日 下午5:07:53
*/

@Controller
public class ImgController {

	@RequestMapping("/imgUpload.do")
	@ResponseBody
	public ImgResult uplpad(MultipartFile file, HttpServletRequest request) throws Exception, IOException {
		String desFilePath = "";
		String oriName = "";
		ImgResult result = new ImgResult();
		Map<String, String> dataMap = new HashMap<>();
		// 1.获取原文件名1
		oriName = file.getOriginalFilename();
		// 2.获取原文件图片后缀，以最后的.作为截取(.jpg)1
		String extName = oriName.substring(oriName.lastIndexOf("."));
		// 3.生成自定义的新文件名，这里以UUID.jpg|png|xxx作为格式（可选操作，也可以不自定义新文件名）1
		String uuid = UUID.randomUUID().toString();
		//组合成新的文件
		String newName = uuid + extName;
		// 4.获取要保存的路径文件夹1
		//String realPath = request.getRealPath("/assets/imgsUpload/");
		String realPath = request.getSession().getServletContext().getRealPath("/assets/imgsUpload/");
		// 5.保存1
		desFilePath = realPath + "\\" + newName;
		File desFile = new File(desFilePath);
		file.transferTo(desFile);
		// 6.返回保存结果信息
		result.setCode(0);
		dataMap = new HashMap<>();
		dataMap.put("src", "/assets/imgsUpload/" + newName);
		result.setData(dataMap);
		return result;
	}	
}
